package labelsystem.Controller;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/28 14:50
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import labelsystem.Mapper.checkMapper;
import labelsystem.Pojo.Check;
import labelsystem.Pojo.Doctor;
import labelsystem.Pojo.Video;
import labelsystem.Pojo.Volunteer;
import labelsystem.Service.doctorService;
import labelsystem.Service.volunteerService;
import labelsystem.utils.returnMSG;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/28 14:50
 *@Version 1.0
 */
@Api("文件上传处理模块")
@RestController
@CrossOrigin
public class fileUpHandler {

    @Resource
    volunteerService volunteerService;
    @Resource
    doctorService doctorService;

    @Resource
    checkMapper checkMapper;

    //志愿者证书上传
    @PostMapping("/certificateV")
    @ApiOperation("志愿者证书上传:vid,file")
    public returnMSG upCertificateV(@RequestParam("vid") String vid, @RequestParam("file")MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("当前证书文件为空");
            return returnMSG;
        }
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "C:/Users/niemengzun/Desktop/test/vCertificate/";

        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = vid+"_certificate" + suffixName;

        // 构建上传路径
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("后端异常");
            return returnMSG;
        }


        /* 更新数据库信息 */
        Volunteer volunteer=volunteerService.queryInfo(Long.parseLong(vid));
        String accessUrl= request.getScheme() + "://" + request.getServerName()
                        + ":" + request.getServerPort() +request.getContextPath() +"/VolunteerCert/"+fileName;
        volunteer.setCertificate(accessUrl);
        volunteerService.updateVolunteer(volunteer);
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.setMsg("上传成功");
        returnMSG.add("volunteer",volunteer);
        return returnMSG;
    }

    //医生证书上传
    @PostMapping("/certificateD")
    @ApiOperation("医生证书上传:did,file")
    public returnMSG upCertificateD(@RequestParam("did") String did, @RequestParam("file")MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("当前证书文件为空");
            return returnMSG;
        }
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "C:/Users/niemengzun/Desktop/test/dCertificate/";

        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = did+"_certificate" + suffixName;

        // 构建上传路径
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
             multipartFile.transferTo(dest);
           } catch (Exception e) {
               e.printStackTrace();
               returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
               returnMSG.setMsg("后端异常");
               return returnMSG;
        }


        /* 更新数据库信息 */
        Doctor doctor=doctorService.queryPersonalInfo(Long.parseLong(did));
        String accessUrl= request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()+request.getContextPath()  +"/doctorCert/"+fileName;
        doctor.setCertificate(accessUrl);
        doctorService.updateInfo(doctor);
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.setMsg("上传成功");
        returnMSG.add("doctor",doctor);
        return returnMSG;
    }

    //医生图片上传
    @PostMapping("/photoUpload")
    @ApiOperation("医生照片上传:did,file")
    public returnMSG photoUpload(@RequestParam("did") String did, @RequestParam("file")MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("图片为空");
            return returnMSG;
        }
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "C:/Users/niemengzun/Desktop/test/doctorPhoto/";

        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = did+"_photo" + suffixName;

        // 构建上传路径
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("后端异常");
            return returnMSG;
        }


        /* 更新数据库信息 */
        Doctor doctor=doctorService.queryPersonalInfo(Long.parseLong(did));
        String accessUrl= request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()  +request.getContextPath()+"/doctorPhoto/"+fileName;
        doctor.setPhotoUrl(accessUrl);
        doctorService.updateInfo(doctor);
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.setMsg("上传成功");
        returnMSG.add("doctor",doctor);
        return returnMSG;
    }

    //检查图片上传
    @PostMapping("/checkload")
    @ApiOperation("检查单上传:did、bindId,file")
    public returnMSG checkload(@RequestParam("did") String did, @RequestParam("bindId") String bindId,@RequestParam("file")MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("文件为空");
            return returnMSG;
        }
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "C:/Users/niemengzun/Desktop/test/check/";

        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = did+"_check" + suffixName;

        // 构建上传路径
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("后端异常");
            return returnMSG;
        }


        /* 更新数据库信息 */
        Check check=new Check();

        String accessUrl= request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() +request.getContextPath() +"/check/"+fileName;
        check.setUrl(accessUrl);
        check.setBindId(Long.parseLong(bindId));
        checkMapper.insert(check);
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("checks",checkMapper.selectList(new QueryWrapper<Check>().eq("bingId",Long.parseLong(bindId))));
        return returnMSG;
    }

    //文章上传
    @PostMapping("/textUpload")
    @ApiOperation("文章上传:id,type(0医生1护工),file")
    public returnMSG textload(@RequestParam("id") String id,@RequestParam("type") String type,@RequestParam("file")MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("文件为空");
            return returnMSG;
        }
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "C:/Users/niemengzun/Desktop/test/text/";

        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = id+"_text" + suffixName;

        // 构建上传路径
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("后端异常");
            return returnMSG;
        }
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        String accessUrl= request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()+request.getContextPath() +"/text/"+fileName;
        if(type.equals("0")){
            doctorService.saveText(Long.parseLong(id),accessUrl);
            returnMSG.add("texts",doctorService.selectTexts(Long.parseLong(id)));
        }else{
            volunteerService.saveText(Long.parseLong(id),accessUrl);
            returnMSG.add("texts",volunteerService.selectTextById(Long.parseLong(id)));
        }
        return returnMSG;
    }



    @PostMapping("/videoUpload")
    @ApiOperation("视频上传:id,type(0医生1护工),file")
    public returnMSG videoload(@RequestParam("id") String id,@RequestParam("type") String type,@RequestParam("file")MultipartFile multipartFile, HttpServletRequest request){
        if(multipartFile.isEmpty()){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("文件为空");
            return returnMSG;
        }
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 上传后的路径
        String filePath = "C:/Users/niemengzun/Desktop/test/video/";

        // 解决中文问题，liunx下中文路径，图片显示问题
        fileName = id+"_video" + suffixName;

        // 构建上传路径
        File dest = new File(filePath + fileName);

        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        try {
            // 保存文件
            multipartFile.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("后端异常");
            return returnMSG;
        }
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        String accessUrl= request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()+request.getContextPath()  +"/video/"+fileName;
        if(type.equals("0")){
            doctorService.saveVideo(Long.parseLong(id),accessUrl);
            returnMSG.add("videos",doctorService.selectRadio(Long.parseLong(id)));
        }else{
            volunteerService.saveText(Long.parseLong(id),accessUrl);
            returnMSG.add("videos",volunteerService.selectVideoById(Long.parseLong(id)));
        }
        return returnMSG;
    }
}
